延时num秒再开始执行线程
from threading import Timer
def fun():
print('hello')
t = Timer(5, fun) # 延时5秒后再开始执行线程
t.start()
1. 使用time模块的sleep延时执行线程 和 使用 threading 中的 Timer 延时执行线程的区别
- 如果延时的时间短,那么就是用 time 中的 sleep
- 如果延时的时间长,那么就是用 threading 中的 Timer
# time.sleep
from threading import Thread
import time
def fun():
time.sleep(5)
print('hello')
t = Thread(target=fun)
t.start()
# threading 中的 Timer
from threading import Timer
def fun():
print('hello')
t = Timer(5, fun) # 延时5秒后再开始执行线程
t.start()
← 算法 线程 - 生产者消费者模型 →